home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src-aux / glaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  9.7 KB  |  458 lines

  1. /* aux.c */
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #if !defined(AMIGA) && !defined(__WIN32__) && !defined(__QUICKDRAW__) && !defined(DOSVGA) && !defined(NeXT) && !defined(__BEOS__)
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/keysym.h>
  11. #endif
  12. #include <GL/gl.h>
  13. #include "gltk.h"
  14. #include "glaux.h"
  15.  
  16. #ifdef AMIWIN
  17. #include <pragmas/xlib_pragmas.h>
  18. extern struct Library *XLibBase;
  19. #endif
  20.  
  21. #if defined(__cplusplus) || defined(c_plusplus)
  22. #define class c_class
  23. #endif
  24.  
  25.  
  26. static struct {
  27.     int keyField;
  28.     void (*KeyFunc)(void);
  29. } keyTable[200];
  30.  
  31. static struct {
  32.     int mouseField;
  33.     void (*MouseFunc)(AUX_EVENTREC *);
  34. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  35.  
  36. static int keyTableCount = 0;
  37. static int mouseDownTableCount = 0;
  38. static int mouseUpTableCount = 0;
  39. static int mouseLocTableCount = 0;
  40. static GLenum displayModeType = 0;
  41. static GLenum displayModePolicy = AUX_MINIMUM_CRITERIA;
  42. static int displayModeID = 0;
  43.  
  44. static int animate = 0;
  45.  
  46.  
  47. #if defined(__WIN32__) || defined(DOSVGA)
  48. #define NCOLORS 17
  49. float auxRGBMap[NCOLORS][3] = {
  50.     {0,0,0},
  51.     {0,0,0},
  52.     {0,0,0},
  53.     {0,0,0},
  54.     {0,0,0},
  55.     {0,0,0},
  56.     {0,0,0},
  57.     {0,0,0},
  58.     {0,0,0},
  59.     {0,0,0},
  60.     {1,0,0},
  61.     {0,1,0},
  62.     {1,1,0},
  63.     {0,0,1},
  64.     {1,0,1},
  65.     {0,1,1},
  66.     {1,1,1}
  67. };
  68. #else
  69. #define NCOLORS 8
  70. float auxRGBMap[NCOLORS][3] = {
  71.     {0,0,0},
  72.     {1,0,0},
  73.     {0,1,0},
  74.     {1,1,0},
  75.     {0,0,1},
  76.     {1,0,1},
  77.     {0,1,1},
  78.     {1,1,1}
  79.  };
  80. #endif
  81.  
  82.  
  83. static void DefaultHandleReshape(int w, int h)
  84. {
  85.     glViewport(0, 0, w, h);
  86.     glMatrixMode(GL_PROJECTION);
  87.     glLoadIdentity();
  88.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  89.     glMatrixMode(GL_MODELVIEW);
  90.     glLoadIdentity();
  91. }
  92.  
  93. static void DefaultHandleExpose(int w, int h)
  94. {
  95. }
  96.  
  97. static GLenum MouseLoc(int x, int y, GLenum button)
  98. {
  99.     AUX_EVENTREC info;
  100.     GLenum flag;
  101.     int i;
  102.  
  103.     flag = GL_FALSE;
  104.     for (i = 0; i < mouseLocTableCount; i++) {
  105.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  106.         info.event = AUX_MOUSELOC;
  107.         info.data[AUX_MOUSEX] = x;
  108.         info.data[AUX_MOUSEY] = y;
  109.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  110.         (*mouseLocTable[i].MouseFunc)(&info);
  111.         flag |= GL_TRUE;
  112.     }
  113.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  114.         info.event = AUX_MOUSELOC;
  115.         info.data[AUX_MOUSEX] = x;
  116.         info.data[AUX_MOUSEY] = y;
  117.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  118.         (*mouseLocTable[i].MouseFunc)(&info);
  119.         flag |= GL_TRUE;
  120.     }
  121.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  122.         info.event = AUX_MOUSELOC;
  123.         info.data[AUX_MOUSEX] = x;
  124.         info.data[AUX_MOUSEY] = y;
  125.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  126.         (*mouseLocTable[i].MouseFunc)(&info);
  127.         flag |= GL_TRUE;
  128.     }
  129.     }
  130.     return flag;
  131. }
  132.  
  133. static GLenum MouseUp(int x, int y, GLenum button)
  134. {
  135.     AUX_EVENTREC info;
  136.     GLenum flag;
  137.     int i;
  138.  
  139.     flag = GL_FALSE;
  140.     for (i = 0; i < mouseUpTableCount; i++) {
  141.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  142.         info.event = AUX_MOUSEUP;
  143.         info.data[AUX_MOUSEX] = x;
  144.         info.data[AUX_MOUSEY] = y;
  145.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  146.         (*mouseUpTable[i].MouseFunc)(&info);
  147.         flag |= GL_TRUE;
  148.     }
  149.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  150.         info.event = AUX_MOUSEUP;
  151.         info.data[AUX_MOUSEX] = x;
  152.         info.data[AUX_MOUSEY] = y;
  153.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  154.         (*mouseUpTable[i].MouseFunc)(&info);
  155.         flag |= GL_TRUE;
  156.     }
  157.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  158.         info.event = AUX_MOUSEUP;
  159.         info.data[AUX_MOUSEX] = x;
  160.         info.data[AUX_MOUSEY] = y;
  161.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  162.         (*mouseUpTable[i].MouseFunc)(&info);
  163.         flag |= GL_TRUE;
  164.     }
  165.     }
  166.     return flag;
  167. }
  168.  
  169. static GLenum MouseDown(int x, int y, GLenum button)
  170. {
  171.     AUX_EVENTREC info;
  172.     GLenum flag;
  173.     int i;
  174.  
  175.     flag = GL_FALSE;
  176.     for (i = 0; i < mouseDownTableCount; i++) {
  177.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  178.         info.event = AUX_MOUSEDOWN;
  179.         info.data[AUX_MOUSEX] = x;
  180.         info.data[AUX_MOUSEY] = y;
  181.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  182.         (*mouseDownTable[i].MouseFunc)(&info);
  183.         flag |= GL_TRUE;
  184.     }
  185.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  186.         info.event = AUX_MOUSEDOWN;
  187.         info.data[AUX_MOUSEX] = x;
  188.         info.data[AUX_MOUSEY] = y;
  189.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  190.         (*mouseDownTable[i].MouseFunc)(&info);
  191.         flag |= GL_TRUE;
  192.     }
  193.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  194.         info.event = AUX_MOUSEDOWN;
  195.         info.data[AUX_MOUSEX] = x;
  196.         info.data[AUX_MOUSEY] = y;
  197.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  198.         (*mouseDownTable[i].MouseFunc)(&info);
  199.         flag |= GL_TRUE;
  200.     }
  201.     }
  202.     return flag;
  203. }
  204.  
  205. static GLenum KeyDown(int key, GLenum status)
  206. {
  207.     GLenum flag;
  208.     int i;
  209.  
  210.     flag = GL_FALSE;
  211.     if (keyTableCount) {
  212.     for (i = 0; i < keyTableCount; i++) {
  213.         if (key == keyTable[i].keyField) {
  214.         (*keyTable[i].KeyFunc)();
  215.         flag |= GL_TRUE;
  216.         }
  217.     }
  218.     }
  219.     return flag;
  220. }
  221.  
  222. void auxExposeFunc(void (*Func)(int, int))
  223. {
  224.     tkExposeFunc(Func);
  225. }
  226.  
  227. void auxReshapeFunc(void (*Func)(int, int))
  228. {
  229.     tkExposeFunc(Func);
  230.     tkReshapeFunc(Func);
  231. }
  232.  
  233. void auxIdleFunc(void (*Func)(void))
  234. {
  235.     tkIdleFunc(Func);
  236. }
  237.  
  238. void auxKeyFunc(int key, void (*Func)(void))
  239. {
  240.     keyTable[keyTableCount].keyField = key;
  241.     keyTable[keyTableCount++].KeyFunc = Func;
  242. }
  243.  
  244. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  245. {
  246.     if (mode == AUX_MOUSEDOWN) {
  247.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  248.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  249.     } else if (mode == AUX_MOUSEUP) {
  250.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  251.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  252.     } else if (mode == AUX_MOUSELOC) {
  253.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  254.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  255.     } 
  256. }
  257.  
  258.  
  259. void auxDeleteMouseFunc( int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  260. {
  261.    int i, j;
  262.  
  263.    for (i=0;i<mouseLocTableCount;i++) {
  264.       if (mouseLocTable[i].MouseFunc == Func) {
  265.          /* delete this one */
  266.          for (j=i+1;j<mouseLocTableCount;j++) {
  267.             mouseLocTable[j-1].MouseFunc = mouseLocTable[j].MouseFunc;
  268.             mouseLocTable[j-1].mouseField = mouseLocTable[j].mouseField;
  269.          }
  270.          mouseLocTableCount--;
  271.          break;
  272.       }
  273.    }
  274.  
  275. }
  276.  
  277.  
  278. static void idle(void)
  279. {
  280.    /* do nothing */
  281. }
  282.  
  283. void auxMainLoop(void (*Func)(void))
  284. {
  285.    if (animate) {
  286.       auxIdleFunc( idle );
  287.    }
  288.  
  289.     tkDisplayFunc(Func);
  290.     tkExec();
  291. }
  292.  
  293. void auxInitPosition(int x, int y, int width, int height)
  294. {
  295.     tkInitPosition(x, y, width, height);
  296. }
  297.  
  298. void auxInitDisplayMode(GLbitfield type)
  299. {
  300.     displayModeType = type;
  301.     tkInitDisplayMode(type);
  302. }
  303.  
  304. void auxInitDisplayModePolicy(GLenum type)
  305. {
  306.  
  307.     displayModePolicy = type;
  308. /*    tkInitDisplayModePolicy(type);*/
  309. }
  310.  
  311. GLenum auxInitDisplayModeID(GLint id)
  312. {
  313.    /* I don't know what this function's supposed to do.  It's just a stub. */
  314.     displayModeID = id;
  315. /*    tkInitDisplayModeID(id);*/
  316.     return (GLenum) id;
  317. }
  318.  
  319. GLenum auxInitWindow(char *title)
  320. {
  321.     int useDoubleAsSingle = 0;
  322.  
  323.     if (tkInitWindow(title) == GL_FALSE) {
  324.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  325.         tkInitDisplayMode(displayModeType|AUX_DOUBLE);
  326.         if (tkInitWindow(title) == GL_FALSE) {
  327.         return GL_FALSE;
  328.         }
  329.         fprintf(stderr, "Can't initialize a single buffer visual.\n");
  330.         fprintf(stderr, "Will use a double buffer visual instead,");
  331.         fprintf(stderr, "only drawing into the front buffer.\n");
  332.         displayModeType = displayModeType | AUX_DOUBLE;
  333.         useDoubleAsSingle = 1;
  334.     }
  335.     }
  336.     tkReshapeFunc(DefaultHandleReshape);
  337.     tkExposeFunc(DefaultHandleExpose);
  338.     tkMouseUpFunc(MouseUp);
  339.     tkMouseDownFunc(MouseDown);
  340.     tkMouseMoveFunc(MouseLoc);
  341.     tkKeyDownFunc(KeyDown);
  342.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  343.     glClearColor(0.0, 0.0, 0.0, 1.0);
  344.     glClearIndex(0);
  345.     glLoadIdentity();
  346.     if (useDoubleAsSingle) {
  347.         glReadBuffer(GL_FRONT);
  348.     glDrawBuffer(GL_FRONT);
  349.     }
  350.     return GL_TRUE;
  351. }
  352.  
  353. void auxCloseWindow(void)
  354. {
  355.     tkCloseWindow();
  356.     keyTableCount = 0;
  357.     mouseDownTableCount = 0;
  358.     mouseUpTableCount = 0;
  359.     mouseLocTableCount = 0;
  360. }
  361.  
  362. void auxQuit(void)
  363. {
  364.     tkQuit();
  365. }
  366.  
  367. void auxSwapBuffers(void)
  368. {
  369.     tkSwapBuffers();
  370. }
  371.  
  372. #if !defined(AMIGA) && !defined(__WIN32__) && !defined(__QUICKDRAW__) && !defined(DOSVGA) && !defined(NeXT) && !defined(__BEOS__)
  373. /* for systems with X only... */
  374. Display *auxXDisplay(void)
  375. {
  376.     Display *ptr;
  377.     
  378.     tkGetSystem(TK_X_DISPLAY, (void *)&ptr);
  379.     return ptr;
  380. }
  381.  
  382. Window auxXWindow(void)
  383. {
  384.     Window ptr;
  385.     
  386.     tkGetSystem(TK_X_WINDOW, (void *)&ptr);
  387.     return ptr;
  388. }
  389. #endif
  390.  
  391. GLenum auxGetDisplayModePolicy(void)
  392. {
  393.    return displayModePolicy;
  394. /*    return tkGetDisplayModePolicy();*/
  395. }
  396.  
  397. GLint auxGetDisplayModeID(void)
  398. {
  399. /*    return tkGetDisplayModeID();*/
  400.    return displayModeID;
  401. }
  402.  
  403. GLenum auxGetDisplayMode(void)
  404. {
  405. /*    return tkGetDisplayMode();*/
  406.    return displayModeType;
  407. }
  408.  
  409. void auxSetOneColor(int index, float r, float g, float b)
  410. {
  411.     tkSetOneColor(index, r, g, b);
  412. }
  413.  
  414. void auxSetFogRamp(int density, int startIndex)
  415. {
  416.     tkSetFogRamp(density, startIndex);
  417. }
  418.  
  419. void auxSetGreyRamp(void)
  420. {
  421.     tkSetGreyRamp();
  422. }
  423.  
  424. void auxSetRGBMap(int size, float *rgb)
  425. {
  426.     tkSetRGBMap(size, rgb);
  427. }
  428.  
  429. int auxGetColorMapSize(void)
  430. {
  431.  
  432.     return tkGetColorMapSize();;
  433. }
  434.  
  435. void auxGetMouseLoc(int *x, int *y)
  436. {
  437.     tkGetMouseLoc(x, y);
  438. }
  439.  
  440. void auxGetScreenSize( GLint *width, GLint *height )
  441. {
  442. #if defined(DOSVGA)
  443.    /* This is a kludge! */
  444.    *width = 1280;
  445.    *height = 1024;
  446. #else
  447.    *width = 320;
  448.    *height = 200;
  449. #endif
  450. }
  451.  
  452.  
  453. void auxAnimation( GLint state )
  454. {
  455.    animate = state;
  456. }
  457.  
  458.